package com.qianyu.project.web.controller.lyz;

import com.qianyu.project.entity.MaterialOutstorageRecord;
import com.qianyu.project.entity.MaterialOutstorageRecordDetail;
import com.qianyu.project.service.IMaterialOutstorageRecordService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/16 19:02
 */
@RestController
@RequestMapping("/MaterialOutstorage")
public class MaterialOutstorageController {
    @Autowired
    private IMaterialOutstorageRecordService service;

    @GetMapping("")
    public Result getOutstorageList(@RequestParam Map<String,String> map){
        return ResultTool.success(service.list());
    }

    @PostMapping("")
    @Transactional
    public Result addOutstorage(@RequestBody Map<String,Object> map){
        System.out.println(map);
        Map<String,Object> outmap= (Map<String, Object>) map.get("out");
        List<Map<String,Object>> detailmaps= (List<Map<String, Object>>) map.get("details");

        MaterialOutstorageRecord record=new MaterialOutstorageRecord();
        record.setFlowId(1);
        record.setProId(Integer.parseInt(outmap.get("pId").toString()));
        record.setOutstorageName(outmap.get("outstorageName").toString());
        record.setApplyer(outmap.get("applyer").toString());
        record.setDate(outmap.get("date").toString());
        record.setGetUser(Integer.parseInt(outmap.get("getUser").toString()));
        record.setGetOrgId(Integer.parseInt(outmap.get("getOrgId").toString()));
        record.setOutstorageId(Integer.parseInt(outmap.get("outstorageId").toString()));
        record.setOutstorageReason(outmap.get("outstorageReason").toString());
        record.setComment(outmap.get("comment").toString());
        record.setApproveStatus(1);

        List<MaterialOutstorageRecordDetail> detailList=new ArrayList<MaterialOutstorageRecordDetail>();
        detailmaps.forEach(d->{
            MaterialOutstorageRecordDetail detail=new MaterialOutstorageRecordDetail();
            detail.setMateId(Integer.parseInt(d.get("mateTypeId").toString()));
            detail.setWarehouseId(Integer.parseInt(d.get("warehouseId").toString()));
            detail.setInstorageCount(Integer.parseInt(d.get("outstorageCount").toString()));
            detail.setPrice(new BigDecimal(d.get("price").toString()));
            detail.setSumAmount(new BigDecimal(detail.getInstorageCount()).multiply(detail.getPrice()));
            detailList.add(detail);
        });
        service.addOutstorage(record,detailList);
        return ResultTool.success();
    }
}
